package com.renli.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.renli.exception.RenLiException;
import com.renli.mapper.StaffMapper;
import com.renli.model.domain.*;
import com.renli.model.dto.StaffDto;
import com.renli.result.R;
import com.renli.service.*;
import com.renli.utils.Date;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
@Slf4j
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements StaffService {
    @Autowired
    private StaffWorkService staffWorkService;
    @Autowired
    private StaffWagesService staffWagesService;
    @Autowired
    private WorkDayService workDayService;
    @Autowired
    private PunishmentService punishmentService;


    @Override
    public R updateOrInsert(StaffDto staffDto,String s) {
        Long id = staffDto.getId();
        if (id == null || id.equals("")) {

            String phone = staffDto.getPhone();
            LambdaQueryWrapper<Staff> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.eq(Staff::getPhone,phone);
            int count = this.count(queryWrapper1);
            if (count!=0){
                throw new RenLiException("该号码已被注册！");
            }
            Staff staff = new Staff();
            BeanUtils.copyProperties(staffDto, staff);
            staff.setCreateTime(LocalDateTime.now());
            staff.setUpdateTime(LocalDateTime.now());
            staff.setUpdatePeople(s);
            staff.setCreatePeople(s);
            staff.setPosition(staffDto.getPosition());
            StaffWork staffWork = new StaffWork();
            boolean save = this.save(staff);
            BeanUtils.copyProperties(staffDto, staffWork);

            LambdaQueryWrapper<Staff> queryWrapper2=new LambdaQueryWrapper<>();
            queryWrapper2.eq(Staff::getPhone,phone);
            Staff one = this.getOne(queryWrapper2);

            //新增staff_wages表
            insertStaffWages(one,s,staffDto);

            //新增workDay表
            insertWorkDay(one);

            Long id1 = one.getId();
            staffWork.setStaffId(id1);
            staffWork.setUpdatePeople(s);
            staffWork.setUpdateTime(LocalDateTime.now());
            boolean save1 = staffWorkService.save(staffWork);
            if (!save || !save1) {
                throw new RenLiException("注册失败");
            }
        } else {
            Staff staff = this.getById(id);
            BeanUtils.copyProperties(staffDto, staff);
            staff.setUpdateTime(LocalDateTime.now());
            staff.setUpdatePeople(s);
            boolean b = this.updateById(staff);
            LambdaQueryWrapper<StaffWork> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(StaffWork::getStaffId, id);
            StaffWork staffWork = staffWorkService.getOne(queryWrapper);
            BeanUtils.copyProperties(staffDto, staffWork,"id");
            staffWork.setUpdateTime(LocalDateTime.now());
            staffWork.setUpdatePeople(s);
            boolean b1 = staffWorkService.updateById(staffWork);
            if (!b || !b1) {
                throw new RenLiException("更新失败");
            }
        }
        return new R(1, "操作成功");
    }

    @Override
    public R getOneById(Long id) {
        if (id==null||id.equals("")){
            throw new RenLiException("未传入员工id");
        }
        StaffDto staffDto=new StaffDto();
        Staff staff = this.getById(id);
        BeanUtils.copyProperties(staff,staffDto);
        LambdaQueryWrapper<StaffWork> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(StaffWork::getStaffId,id);
        StaffWork staffWork = staffWorkService.getOne(queryWrapper);
        BeanUtils.copyProperties(staffWork,staffDto);
        log.info("啦啦啦{}",staffDto);
        return new R(1,staffDto);
    }

    /**
     * 删除员工全部信息
     * @param id
     */
    @Override
    public void deleteStaffById(Long id) {
        Staff staff = this.getById(id);
        if (id == null || id.equals("")) {
            throw new RenLiException("删除失败");
        }
        boolean b = this.removeById(id);
        if (!b){
            throw new RenLiException("删除Staff表失败");
        }
        LambdaQueryWrapper<StaffWork> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StaffWork::getStaffId, id);
        StaffWork staffWork = staffWorkService.getOne(queryWrapper);
        boolean b1 = staffWorkService.removeById(staffWork.getId());
        if (!b1) {
            this.save(staff);
            throw new RenLiException("删除staffWork表失败");
        }

        LambdaQueryWrapper<StaffWages> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(StaffWages::getStaffId,id);
        StaffWages staffWages = staffWagesService.getOne(queryWrapper1);
        boolean remove = staffWagesService.remove(queryWrapper1);
        if (!remove){
            this.save(staff);
            staffWorkService.save(staffWork);
            throw new RenLiException("删除StaffWages表失败");
        }

        LambdaQueryWrapper<WorkDay> queryWrapper2=new LambdaQueryWrapper<>();
        queryWrapper2.eq(WorkDay::getStaffId,id);
        WorkDay workDay = workDayService.getOne(queryWrapper2);
        boolean remove1 = workDayService.remove(queryWrapper2);
        if (!remove1){
            this.save(staff);
            staffWorkService.save(staffWork);
            staffWagesService.save(staffWages);
            throw new RenLiException("删除workDay表失败");
        }

        LambdaQueryWrapper<Punishment> queryWrapper3=new LambdaQueryWrapper<>();
        queryWrapper3.eq(Punishment::getStaffId,id);
        int count = punishmentService.count(queryWrapper3);
        if (count != 0) {
            boolean remove2 = punishmentService.remove(queryWrapper3);
            if (!remove2){
                this.save(staff);
                staffWorkService.save(staffWork);
                staffWagesService.save(staffWages);
                workDayService.save(workDay);
                throw new RenLiException("删除punishment表失败");
            }
        }

    }


    private void insertStaffWages(Staff staffDto,String s,StaffDto pay){
        Long id = staffDto.getId();
        StaffWages staffWages=new StaffWages();
        String name = staffDto.getName();
        staffWages.setStaffId(id);
        staffWages.setName(name);
        staffWages.setUpdatePeople(s);
        if (pay.getBasicWage()!=null) {
            staffWages.setBasicWage(pay.getBasicWage());
        }
        if (pay.getPostPay()!=null){
        staffWages.setPostPay(pay.getPostPay());}

        staffWages.setUpdateTime(LocalDateTime.now());
        boolean save = staffWagesService.save(staffWages);
        if (!save){
            throw new RuntimeException("新增staffWages表失败");
        }
        LambdaQueryWrapper<StaffWages> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(StaffWages::getStaffId,id);
        StaffWages one = staffWagesService.getOne(queryWrapper);
        Double all= pay.getBasicWage()+one.getFullAttendenceAward()
               +pay.getPostPay()* Date.getCurrentMonthLastDay();
        one.setThisMonthsSalary(all);
        boolean b = staffWagesService.updateById(one);
        if (!b){
            throw new RuntimeException("添加本月工资失败");
        }
    }
    private void insertWorkDay(Staff staffDto){
        Long id = staffDto.getId();
        String name = staffDto.getName();
        WorkDay workDay=new WorkDay();
        workDay.setStaffId(id);
         workDay.setName(name);
        boolean save = workDayService.save(workDay);
        if (!save){
            throw new RenLiException("新增workDay表失败");
        }
    }
}
